package org.apereo.cas.web.y9.config;

import org.apereo.cas.authentication.CasSSLContext;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.redis.core.RedisObjectFactory;
import org.apereo.cas.web.y9.util.Y9Context;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import lombok.val;

@Configuration(proxyBeanMethods = false)
public class Y9RedisConfig {

    @Bean
    @RefreshScope
    @ConditionalOnMissingBean(RedisConnectionFactory.class)
    public RedisConnectionFactory redisConnectionFactory(CasConfigurationProperties casProperties) {
        val redis = casProperties.getTicket().getRegistry().getRedis();
        return RedisObjectFactory.newRedisConnectionFactory(redis, true, Y9Context.getBean(CasSSLContext.BEAN_NAME));
    }

    @Bean
    @RefreshScope
    public RedisTemplate<String, Object> y9RedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        return RedisObjectFactory.newRedisTemplate(redisConnectionFactory);
    }

}
